//
//  MaintenceCalendarScrollView.swift
//  koolcar
//
//  Created by xiantian on 2023/10/24.
//

import UIKit


class MaintenceCalendarScrollView: UIView {
    var curPage: NSInteger = 0
    var scrollView: UIScrollView!
    var curDateLab: UILabel!
    var headerView: UIView!
//    var dataArray: [CalendarItemData] = [] {
//        didSet {
//            self.initialData()
//        }
//    }
    
    var leftBtn: UIButton!
    var rightBtn: UIButton!
    override init(frame: CGRect) {
        super.init(frame: frame)
        headerView = self.createHeaderView()
        self.addSubview(headerView)
        
        scrollView = UIScrollView.init()
        scrollView.isScrollEnabled = true
        scrollView.isPagingEnabled = true
        scrollView.delegate = self
        scrollView.bounces = false
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        self.addSubview(scrollView)
        
        self.addSnpLayout()
    }
    
    private func createHeaderView() -> UIView {
        let tempView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: kScreenWidth, height: 40))
        tempView.backgroundColor = .white
      
        leftBtn = UIButton()
        leftBtn.setup_image(image: UIImage.init(named: "calendar_left"))
        leftBtn.addTarget(self, action: #selector(leftBtnAction), for: .touchUpInside)
        tempView.addSubview(leftBtn)
        
        rightBtn = UIButton()
        rightBtn.setup_image(image: UIImage.init(named: "calendar_right"))
        rightBtn.addTarget(self, action: #selector(rightBtnAction), for: .touchUpInside)
        tempView.addSubview(rightBtn)
        
        curDateLab = UILabel()
        curDateLab.textColor = Theme.titleLabColor
        curDateLab.backgroundColor = UIColor.clear
        curDateLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 16)
        curDateLab.numberOfLines = 1
        curDateLab.lineBreakMode = .byTruncatingTail
        curDateLab.textAlignment = .center
        curDateLab.text = ""
        tempView.addSubview(curDateLab)
        
        
        let lineView = UIView()
        lineView.backgroundColor = Theme.lineColor
        tempView.addSubview(lineView)
        
        
        leftBtn.snp.makeConstraints { make in
            make.left.equalTo(4)
            make.top.equalTo(0)
            make.width.equalTo(40)
            make.height.equalTo(40)
        }
        
        
        rightBtn.snp.makeConstraints { make in
            make.right.equalTo(-4)
            make.top.equalTo(0)
            make.width.equalTo(40)
            make.height.equalTo(40)
        }
        
        curDateLab.snp.makeConstraints { make in
            make.left.equalTo(leftBtn.snp.right)
            make.right.equalTo(rightBtn.snp.left)
            make.top.equalTo(0)
            make.height.equalTo(40)
        }
        lineView.snp.makeConstraints { make in
            make.left.right.equalTo(0)
            make.height.equalTo(0.5)
            make.bottom.equalTo(-0.5)
        }
        tempView.layoutIfNeeded()
        return tempView
        
    }
  
    private func addSnpLayout(){
        scrollView.snp.makeConstraints { make in
            make.left.right.equalTo(0)
            make.top.equalTo(40)
            make.bottom.equalTo(0)
        }
        self.layoutIfNeeded()
    }
    
    @objc func leftBtnAction(){
        if self.curPage > 0 {
            self.curPage = self.curPage - 1
            if self.dataArray.count > self.curPage {
                self.curDateLab.text = self.dataArray[self.curPage].date ?? ""
            }
            let contentOffX = CGFloat(self.curPage) * kScreenWidth
            UIView.animate(withDuration: 0.2) {
                self.scrollView.contentOffset.x = contentOffX
            }
        }
        self.setArrowAlpa()
    }
    
    @objc func rightBtnAction(){
        let totalPage = self.dataArray.count
        if self.curPage < (totalPage - 1) {
            self.curPage = self.curPage + 1
            let contentOffX = CGFloat(self.curPage) * kScreenWidth
            UIView.animate(withDuration: 0.2) {
                self.scrollView.contentOffset.x = contentOffX
            }
            
            if self.dataArray.count > self.curPage {
                self.curDateLab.text = self.dataArray[self.curPage].date ?? ""
            }
        }
        self.setArrowAlpa()
    }
   
    private func initialData(){
        guard self.dataArray.count > 0 else {
            return
        }
        if self.dataArray.count > self.curPage {
            self.curDateLab.text = self.dataArray[self.curPage].date ?? ""
        }
        for (index, data) in self.dataArray.enumerated() {
            let calendar = MaintenceCalendarView()
            calendar.data = data
            calendar.frame = CGRect.init(x: CGFloat(index) * kScreenWidth, y: 0, width: kScreenWidth, height: 7 * 53)
            self.scrollView.addSubview(calendar)
        }
        self.setArrowAlpa()
        self.scrollView.contentSize = CGSize.init(width: CGFloat(self.dataArray.count) * kScreenWidth, height: self.scrollView.frame.size.height)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()

    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setArrowAlpa(){
        let tempList = self.dataArray
        if tempList.count == 0 {
            return
        }
        
        if tempList.count == 1 {
            self.leftBtn.alpha = 0.5
            self.rightBtn.alpha = 0.5
            return
        }
        
        if self.curPage == 0 {
            self.leftBtn.alpha = 0.5
            self.rightBtn.alpha = 1
        }else{
            self.leftBtn.alpha = 1
            if self.curPage == tempList.count - 1 {
                self.rightBtn.alpha = 0.5
            }else{
                self.rightBtn.alpha = 1
            }
        }
    }
}


extension MaintenceCalendarScrollView: UIScrollViewDelegate {
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let offsetX = scrollView.contentOffset.x
        let page = Int(offsetX/kScreenWidth)
        self.curPage = page
        if self.dataArray.count > page {
            self.curDateLab.text = self.dataArray[self.curPage].date ?? ""
        }
        self.setArrowAlpa()
    }
}


